import React from 'react';
import ReactDOM from 'react-dom/client';
import './sass/index.scss';
import App from './App';
import 'lib-flexible/flexible';
import { HashRouter } from 'react-router-dom'
import { Provider } from 'react-redux'
import store from './store/store'
import axios from 'axios';
import { Toast } from 'react-vant';
import { urls } from './utils/urls';


const appkey = 'U2FsdGVkX19WSQ59Cg+Fj9jNZPxRC5y0XB1iV06BeNA='
axios.defaults.baseURL = 'http://www.kangliuyong.com:10002'

// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  Toast.loading({
    message: '加载中...',
    forbidClick: true,
    duration: 0
  })

  let token = localStorage.getItem('token')

  if (config.method == 'get') {
    config.params = config.params || {}
    config.params.appkey = appkey
    if (urls.indexOf(config.url) > -1) {
      config.params.tokenString = token
    }
  } else if (config.method == 'post') {

    config.data = config.data || {}
    let dataString = ''

    if (config.data) {
      for (let key in config.data) {
        dataString += `${key}=${config.data[key]}&`
      }
    }

    if (dataString) {
      dataString += `appkey=${appkey}`
    }

    if (urls.indexOf(config.url) > -1) {
      dataString += `&tokenString=${token}`
    }

    config.data = dataString
  }
  return config;
}, function (error) {
  // 对请求错误做些什么
  Toast.clear()
  return Promise.reject(error);
});

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
  Toast.clear()
  return response;
}, function (error) {
  // 对响应错误做点什么
  Toast.clear()
  return Promise.reject(error);
});

const root = ReactDOM.createRoot(document.getElementById('root'));
root.render(
  <Provider store={store}>
    <HashRouter>
      <App />
    </HashRouter>
  </Provider>
);

